package com.senseauto.mediarecorder;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;



import java.util.ArrayList;
import java.util.List;

/**
 * 这个是申超给的aar测试，说说针对摄像头的封装类，用来测试aar是否可用以及如何用的
 */
public class MainActivity extends AppCompatActivity {

    private String[] permissions = new String[]{Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO};
    private List<String> mPermissionList = new ArrayList<>();
    private int PERMISSION_REQUEST_CODE = 100;

    private boolean hasAllPermission() {
        mPermissionList.clear();
        if (Build.VERSION.SDK_INT < 23) {
            return true;
        }
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        return mPermissionList.size() <= 0;

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean hasPermissionDismiss = false;
        if (requestCode == PERMISSION_REQUEST_CODE) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    hasPermissionDismiss = true;
                }
            }

            if (hasPermissionDismiss) {
                Toast.makeText(this, "请授予必要权限", Toast.LENGTH_SHORT).show();
            } else {

            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!hasAllPermission()) {
            ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST_CODE);
        }else{ //第一次赋值过权限后，第二次打开界面
            //startCamera();
        }
        findViewById(R.id.go1).setOnClickListener(view->{
            Toast.makeText(getApplicationContext(), "go RecordActivity", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getApplicationContext(), RecordActivity.class);
            startActivity(intent);
        });

    }
}